<?php
namespace Admin\Controller;

class CostServerController extends Controller {

    public function index() {
        $m    = D2();
        $page = $m->where($this->indexExpr)->order($this->sortExpr)->page2();

        $this->assign('page', $page)->display();
    }

    public function cleanStatsHistory() {
        $ignoreSchema = array(
            'public',
            'm' . date('Ym'),   //忽略当月的按月创建数据的模式
            'm' . date('Ym', strtotime('-1 month')),    //忽略前一个月的按月创建的模式
        );

        for($i = 0; $i < 8; $i++) {
            $ignoreSchema[] = 'd' . date('Ymd', strtotime("-{$i} day"));
        }

        $ignoreSchemaStr = "'" . implode("','", $ignoreSchema) . "'";
        $sql = 'SELECT nspname AS name FROM  pg_database AS A LEFT JOIN  pg_catalog.pg_namespace AS B ON (nspowner=datdba) ' 
             . "WHERE datname='" . C('DB_NAME') . "' AND nspname NOT IN ({$ignoreSchemaStr})";
        $r = M('Model')->query($sql);

        $this->assign('record', $r)->display();
    }

    /**
     * doCleanStatsHistory
     * 删除过期数据模式
     *
     * @return void
     */
    public function doCleanStatsHistory() {
        $d = $_POST['d'];

        if(empty($d) || !is_array($d)) {
            showJson(array('s' => false, 'm' => '请选择需要删除的数据模式'));
        }

        $maxDayTimestamp   = strtotime('-7 day 0:0');
        $maxMonthTimestamp = strtotime(date('Y-m-1', strtotime('-1 month')));
        $daySchemaTpl      = 'd%4d%2d%2d';
        $monthSchemaTpl    = 'm%4d%2d';
        $model             = M('Model');

        foreach($d as $k => $v) {
            switch(substr($v, 0, 1)) {
                case 'd':
                    $tmp          = sscanf($v, $daySchemaTpl);
                    $tmpTimestamp = strtotime(implode('-', $tmp));

                    if($tmpTimestamp >= $maxDayTimestamp) {
                        continue 2; //switch也表示一种循环，如果直接写continue表示退出switch，continue 2表示跳转到第2层循环
                    }
                    break;

                case 'm':
                    $tmp          = sscanf($v, $monthSchemaTpl);
                    $tmpTimestamp = strtotime(implode('-', $tmp) . '-1');

                    if($tmpTimestamp >= $maxMonthTimestamp) {
                        continue 2;
                    }
                    break;

                default:
                    continue;
            }

            $sql = "DROP SCHEMA IF EXISTS {$v} CASCADE";
            $model->execute($sql);
        }

        showJson(array('s' => true, 'm' => '清除成功'));
    }

    public function doSave() {
        $d = $_POST;

        try {
            $r = D2()->save2($d)
                 ? array('s' => true, 'm' => '保存成功')
                 : array('s' => false, 'm' => '保存出错');
        } catch(Exception $e) {
            $r = array('s' => false, 'm' => $e->getMessage());
        }

        showJson($r);
    }

    public function modify() {
        $id = empty($_GET['id']) ? null : (int)$_GET['id'];

        if(empty($id)) {
            exit('参数不完整');
        }

        if(!$r = D2()->getById($id)) {
            exit('没有找到相应服务器数据');
        }

        $this->assign('record', $r)->display();
    }

    public function del() {
        $id = empty($_GET['id']) ? null : (int)$_GET['id'];

        if(empty($id)) {
            showJson(array('s' => false, 'm' => '参数不完整'));
        }

        D2()->where(array('id' => $id))->delete();

        showJson(array('s' => true, 'm' => '删除成功'));
    }

}
